class DataType(object):
    def __init__(self, _type=0, parse=None):
        """
        type 分为三类：
        0:可以直接使用
        1:可枚举值,需要使用map来进行替换
        2:带解析值,需要解析函数来解析
        :param _type:
        :param parse:
        """
        self._type = _type
        self.parse = parse

    def __repr__(self):
        return str({"type": self._type, "parse": self.parse})


parse = {
    "loan_amnt": DataType(0, parse=lambda x: float(x)),  # 贷款金额
    "term": DataType(2, parse=lambda x: float(x.replace("months", ""))),  # 贷款周期，单位 months
    "int_rate": DataType(2, parse=lambda x: float(x.replace("%", ""))),  # 贷款利率
    "installment": DataType(0, parse=lambda x: float(x)),  # 分期付款金额
    "grade": DataType(1, parse={
        "A": 0,
        "B": 1,
        "C": 2,
        "D": 3,
        "E": 4,
        "F": 5,
        "G": 6,
    }),  # 贷款等级
    "home_ownership": DataType(1, parse={
        "RENT": 0,
        "MORTGAGE": 1,
        "OWN": 2,
        "ANY": 3
    }),  # 房产所有权
    "annual_inc": DataType(0, parse=lambda x: float(x)),  # 年收入
    "verification_status": DataType(1, parse={
        "Not Verified": 0,
        "Verified": 1,
        "Source Verified": 2
    }),  # 收入来源是否已经验证
    "loan_status": DataType(1, parse={
        "Default": 1,
        "Charged Off": 1,
        "Fully Paid": 1,
        "In Grace Period": 1,
        "Late (16-30 days)": 0,
        "Late (31-120 days)": 0,
    }),  # 贷款状况
    "purpose": DataType(1, parse={
        "car": 0,
        "credit_card": 1,
        "home_improvement": 2,
        "debt_consolidation": 3,
        "house": 4,
        "major_purchase": 5,
        "medical": 6,
        "moving": 7,
        "other": 8,
        "vacation": 9,
        "small_business": 10,
        "renewable_energy": 11
    }),  # 贷款目的
    "dti": DataType(0, parse=lambda x: float(x)),  # 债务收入比
    "total_acc": DataType(0, parse=lambda x: float(x)),  # 信用额度数
    "revol_util": DataType(2, parse=lambda x: float(x.replace("%", "")))  # 循环贷款利用率
}

keys = ["loan_amnt", "term", "int_rate",
        "installment", "grade", "home_ownership",
        "annual_inc", "verification_status", "loan_status",
        "purpose", "dti", "total_acc", "revol_util"]


def map_value_to_list(map):
    ls = []
    for key in keys:
        ls.append(map[key])
    return ls


def load_csv(path):
    data_set = []
    parse_types = map_value_to_list(parse)
    with open(path) as file:
        file.readline()  # 跳过第一行
        while True:
            new_line = []
            line = file.readline().split(",")

            if not line or len(line) == 1:
                break

            for i in range(len(line)):
                parse_type = parse_types[i]
                value = line[i]
                if parse_type._type in [0, 2]:
                    new_line.append(parse_type.parse(value))
                else:
                    new_line.append(parse_type.parse[value])
            data_set.append(new_line)
    return data_set


def split_xy(data_set):
    x = []
    y = []
    for line in data_set:
        x.append(line[0:8] + line[9:])
        y.append(line[8])
    return x, y

from pandas import DataFrame

data_set = load_csv("loanStaus_new.csv")
frame = DataFrame(data_set, columns=keys)
frame.to_csv("data/0_1.csv")

